/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sync.services;

import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author yarden
 */
public class AssignmentItemParser {
    public static final String TYPE_TAG             = "type";
    public static final String CONTENT_TAG          = "content";
    public static final String TEXT_TYPE_ATTRIBUTE  = "Text Document";
    
    private XMLEventReader reader;
    private XMLEvent event;
    
    public String parse(InputStream input) {
        XMLInputFactory factory = getFactory();
        
        try {
            reader = factory.createXMLEventReader(input);
            
            while (reader.hasNext()) {
                event = reader.nextEvent();
                if (event.isStartElement()) {
                    StartElement start = event.asStartElement();
                    String name = start.getName().getLocalPart();
                    
                    if (name.equals(TYPE_TAG)) {
                        event = reader.nextEvent();
                        if (event.isCharacters()) {
                            String type = event.asCharacters().toString();
                            if (!type.equals(TEXT_TYPE_ATTRIBUTE)) {
                                Logger.getLogger(AssignmentItemParser.class.getName()).log(Level.SEVERE, "Can not handle UoA of type:",type);
                                return "";
                            }
                        }
                    }
                    else if (name.equals(CONTENT_TAG)) {
                        event = reader.nextEvent();
                        if (event.isCharacters())
                            return event.asCharacters().toString();
                    }
                }
            }
            
        } catch (XMLStreamException e) {
            Logger.getLogger(AssignmentItemParser.class.getName()).log(Level.SEVERE, "While reading server response", e);
        }
        
        Logger.getLogger(AssignmentItemParser.class.getName()).log(Level.WARNING, "Unit of analysis without context");
        return "";
        
    }
    
    private XMLInputFactory getFactory() {
        XMLInputFactory factory = null;
        try {
            factory = XMLInputFactory.newInstance();
            factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
            factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
            factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
            factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        } catch (Exception e) {
            Logger.getLogger(AssignmentsParser.class.getName()).log(Level.SEVERE, null, e);
        }
        return factory;
    }
}
